# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=newlib
pkgver=4.4.0.20231231
pkgrel=0
pkgdesc="C standard library implementation intended for use on embedded systems"
url="https://www.sourceware.org/newlib/"
makedepends="texinfo"
arch="aarch64 ppc64le x86_64 x86"
license="GPL-2.0-only AND LGPL-2.1-only AND 0BSD AND BSD-3-Clause AND GPL-3.0-only AND LGPL-3.0-only"
source="
	https://sourceware.org/pub/newlib/newlib-$pkgver.tar.gz

	msp430-elf-nano.specs
	0001-msp430-enable-syscalls.patch
	0002-or1k-missing-include.patch
	0003-or1k-fix-uart-irq-handler-signature.patch
	0004-or1k-drop-invalid-cast.patch
	0005-or1k-add-missing-cast.patch
	"
options="!check"

# classify targets as exotic or mainstream. Only build support for
# exotic targets on typical developer / CI hardware (x86_86, aarach64).
_targets_mainstream="
	arm-none-eabi
	riscv-none-elf
	"

_targets_exotic="
	msp430-elf
	or1k-elf
	aarch64-none-elf
	"

case "$CARCH" in
x86_64|aarch64)
	# support all targets on typical workstation / notebook / CI servers
	_targets="$_targets_mainstream $_targets_exotic"
	;;
*)
	# support only mainstream targets on exotic hosts
	_targets="$_targets_mainstream"
	;;
esac

for target in $_targets; do
	subpackages="newlib-$target:_install_subpkg $subpackages"
	# here only makedepends. The depends to binutils-$target and gcc-$target
	# will be set in the subpkg newlib-$target
	makedepends="binutils-$target gcc-$target $makedepends"
done

# secfixes:
#   4.1.0-r0:
#     - CVE-2021-3420

# This package installs two version of the newlib library to remain compatible
# with projects expecting the GNU ARM Embedded Toolchain [1]. The nano version
# appends the postfix "_nano" to the libraries. The is not an arbitrary choice,
# it matches the specs in "/usr/arm-none-eabi/lib/nano.specs", which is
# part of the default newlib installation.
# [1]: https://launchpad.net/gcc-arm-embedded/

build() {
	for target in $_targets; do
		mkdir "$builddir/build-$target" "$builddir/build-$target-nano"
		# nano version
		cd "$builddir/build-$target-nano"
		export CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections -fshort-wchar"
		"$builddir"/configure \
				--host="$CHOST" \
				--target="$target" \
				--prefix=/usr/ \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-fseek-optimization \
				--disable-newlib-fvwrite-in-streamio \
				--disable-newlib-supplied-syscalls \
				--disable-newlib-unbuf-stream-opt \
				--disable-newlib-wide-orient \
				--disable-nls \
				--enable-lite-exit \
				--enable-newlib-global-atexit \
				--enable-newlib-nano-formatted-io \
				--enable-newlib-nano-malloc \
				--enable-newlib-reent-small \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make

		# regular version
		cd "$builddir/build-$target"
		export CFLAGS_FOR_TARGET="-O2 -ffunction-sections -fdata-sections"
		"$builddir"/configure \
				--host="$CHOST" \
				--target="$target" \
				--prefix=/usr \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-supplied-syscalls \
				--disable-nls \
				--enable-newlib-io-c99-formats \
				--enable-newlib-io-long-long \
				--enable-newlib-register-fini \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make
	done
}

package() {
	for target in $_targets; do
		depends="$depends newlib-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	local target
	target="${subpkgname#newlib-}"
	pkgdesc="A C standard library implementation intended for $target"
	depends="binutils-$target gcc-$target $depends"
	mkdir -p "$subpkgdir"
	# install nano version
	cd "$builddir/build-$target-nano"
	make -j1 DESTDIR="$subpkgdir" install
	# rename nano version libs
	cd "$subpkgdir/usr/$target/lib"
	find . -regex ".*\(c\|g\|m\|rdimon\|gloss\)\.a" | while read -r _file; do
		mv "$_file" "${_file%%.a}_nano.a"
	done
	mkdir -p "$subpkgdir/usr/$target/include/newlib-nano"
	mv "$subpkgdir/usr/$target/include/newlib.h" \
	   "$subpkgdir/usr/$target/include/newlib-nano/"

	# install regular version
	cd "$builddir/build-$target"
	make -j1 DESTDIR="$subpkgdir" install
	rm -rf "$subpkgdir/deleteme"

	# install custom nano specs, if existing
	if [ -e "$srcdir/$target-nano.specs" ]; then
		install -m 644 "$srcdir/$target-nano.specs" \
			"$subpkgdir/usr/$target/lib/nano.specs"
	fi
}

sha512sums="
ea3baa0b7c9175aae024f0b7d272be092ef2c07483239a99329203e18a44bc23093d29e0ffcbe14bc591f610f0829eacd646cabb06d1c34aa23239cb1b814b46  newlib-4.4.0.20231231.tar.gz
647b79e3cc6f3bb98b1263ebbf4b3efa4a4692fddc0df6491c726de2fa0ee46b05ab2ddf96c2c8c785f3cd6a442135ec97bf8df93a8f371d479ea46d03aa8a08  msp430-elf-nano.specs
268c44f8056067e03ee9914d04e8cfa2bb22764bb294b7d16dbfdd51c2e5ef0ad221037a86caafd20ab755b16b38519d61e27ffcd13239cbdbbfad421fa8cb60  0001-msp430-enable-syscalls.patch
f48058084d25d59edf4d4bd4ffc23428a4a89e00c9f6ed7588315ddb5bafcc68e2b63945b135e3ba0128d108d410ec042e27f32c79dbd3a0bcc9b21c2ba236f7  0002-or1k-missing-include.patch
780d4c8e3b90f875044891a03f8215a7ea857601c908434d31691c47da09af967e264a0f350e165a7b3c674fb7b49421bde96acd9a8a7936a549e53ae79e8acd  0003-or1k-fix-uart-irq-handler-signature.patch
0a8b9ea27998baab7f499f45fe8b3e537919142e1372055fef7ed221428c2e02fc00ec21470c8820115ea65f20d23050ae3f0639094aed1368d1447d293f8382  0004-or1k-drop-invalid-cast.patch
997fcf0dc49d0f92e49c8148959e9979a7721b2da1defa0ffc67379b028ef66a454b7582f06b9965280fe9c58cf65b8f4f4127490cb7e6cd209be3b244bb65ae  0005-or1k-add-missing-cast.patch
"
